﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Collections;
using MySql.Data.MySqlClient;
using System.Data.OleDb;
using h = functionService.shorts;
using da = functionService.DATECOD;
using fr = HK.FiscalRegistrar.FiscalRegistrar;
using vv = functionService.serfun_city7;
using rz = functionService.serfun_city7.rz;
using nameCod = functionService.nameCod;


namespace city7
{
    enum opro { o, p, r, opn, pp, rp, opk, ofk, list_doc_month, look_doc, look_ct_in_doc_r, look_ct_in_doc_rp,
    look_doc_r, look_doc_r_ct, look_doc_rp, look_doc_rp_ct, look_doc_p, look_doc_pp, look_All_rezpt, look_All_rezpt_ct, look_All_rezptBPeriod,
    look_All_rezpt_BPeriod_ct, lookDoc_PRIHOD, lookDoc_RASHOD, lookDoc_RASHODdays, lookDoc_RASHODnr, look_SirPer, look_SirPer_Before,
    look_r4_rdtd_prod, look_ost_workDay_s2_1__param2, lookAvgPr, lookOstN900, lookAnyDoc
    }
    enum modeLookDoc { filterCt, fullDoc}

    enum reports { allDocTD_group, allDocTD, fromGrid, fromGridClass }

    public partial class FormDocLook : Form
    {
        functionService.prm_city q;
        rzpt.modeWAED currentMode;
        DataTable dts; opro currentOpro;   //modeLookDoc currentModeLookDoc;
        reports currentReport;
        string sds, sdp, ds, ds1, dp, sm, codd, codo;
        string ct;

        #region declaration for print

        Rectangle[] rra; int[] wwa;
        int currentPage, linesPrint;
        #endregion


        public FormDocLook()
        {
            InitializeComponent(); 
        }
        public FormDocLook(int opro0): this()
        {
            InitializeComponentMy((opro)opro0);
        }
        public FormDocLook(int opro0, object ct0)
            : this()
        {
            InitializeComponentMy((opro)opro0, ct0);
        }
        private void InitializeComponentMy(opro opro0)
        {
            q = new functionService.prm_city(); //q.con.co(q.tk(ETALON.param.server_1)); q.tk

            string s = q.tk((int)ETALON.param.server_1);
            q.con.co(s);

            ct = "888";
            sds = q.tk(ETALON.param.period_s_5);
            sdp = q.tk(ETALON.param.period_p_6);
            ds = Convert.ToDateTime(sds).Day.ToString();
            dp = Convert.ToDateTime(sdp).Day.ToString();
            ds1 = ( h.i( ds) > 1 ? (h.i(ds) - 1) : 1).ToString();

            sm = q.tk(ETALON.param.kafe_smena_42).Substring(0, 3);
            currentOpro = opro0; currentMode = city7.rzpt.modeWAED.work;
            load(opro0);

            setMode(city7.rzpt.modeWAED.work); set();

            vv.rz.currentColumn = 0;
            vv.rz.currentRow = 0;

            codd = "0";
              

        }
        private void InitializeComponentMy(opro opro0, object ct0)
        {
            q = new functionService.prm_city();

            string s = q.tk((int)ETALON.param.server_1);
            q.con.co(s);

            ct = ct0.ToString();
            sds = q.tk(ETALON.param.period_s_5);
            sdp = q.tk(ETALON.param.period_p_6);
            ds = Convert.ToDateTime(sds).Day.ToString();
            dp = Convert.ToDateTime(sdp).Day.ToString();
            sm = q.tk(ETALON.param.kafe_smena_42).Substring(0, 3);
            currentOpro = opro0;
            //H.m(currentOpro.ToString(), " ", ct0);
            load( opro0,  ct0);

            setMode(city7.rzpt.modeWAED.work); set();

            vv.rz.currentColumn = 0;
            vv.rz.currentRow = 0;

            currentPage = 0; linesPrint = 0;
            
        }

        void cellclick()
        {
            if (currentMode == city7.rzpt.modeWAED.add)
            {

            }
            else if (currentMode == city7.rzpt.modeWAED.edit)
            {
                cellclick_edit();
            }
            else if (currentMode == city7.rzpt.modeWAED.delete)
            {

            }
            else if (currentMode == city7.rzpt.modeWAED.work)
            {
                cellclick_work();
            }
            else if (currentMode == city7.rzpt.modeWAED.non)
            {

            }
        }
        private void cellclick_edit()
        {
            if (currentOpro == opro.lookOstN900 || currentOpro == opro.lookAnyDoc )
            {

                /*SELECT name_cod(
        n.name
        , ct) as name, kol, kol2, pr, kol*kol2*pr as sd ,codd, s.cod123, ct
    FROM sir_td_z s
    left join city.',sTableName,' n on s.ct= n.cod
    where codd = ', coddo ,' order by s.cod123 desc;*/
                int xx = dataGridView1.SelectedCells[0].ColumnIndex;
                object y = dataGridView1.SelectedCells[0].Value;
                int yy = dataGridView1.SelectedCells[0].RowIndex;
                object o123 = dataGridView1.Rows[yy].Cells[6].Value;
                object ov = "";
                string codSc = nameCod.readCod(dataGridView1.Rows[yy].Cells[0].Value.ToString());

                string sql = "";

                if (xx == 0) { }  //наименование продукции
                else if (xx == 1)  //кол 1
                {
                    ov = dataGridView1.Rows[yy].Cells[1].Value;
                    ETALON.FormKolZakaz fk = new ETALON.FormKolZakaz("Введите количество", ov.ToString()); fk.ShowDialog();
                    if (fk.IsSelect)
                    {
                        sql = string.Concat("UPDATE city.sir_td_z SET kol=", fk.valueEnter.ToString().Replace(",","."), " WHERE cod123= ", o123, ";"); q.UpdateData(sql);
                        toolStripTextBox1.Text = codSc;
                        dataGridView1.SelectedCells[0].Value = Convert.ToDouble(fk.valueEnter.Replace(".", ","));
                    }
                }
                else if (xx == 2)  //кол 2
                {
                    ov = dataGridView1.Rows[yy].Cells[2].Value;
                    ETALON.FormKolZakaz fk = new ETALON.FormKolZakaz("Введите количество", ov.ToString()); fk.ShowDialog();
                    if (fk.IsSelect)
                    {
                        sql = string.Concat("UPDATE city.sir_td_z SET kol2=", fk.valueEnter.ToString().Replace(",", "."), " WHERE cod123= ", o123, ";"); q.UpdateData(sql);
                        toolStripTextBox1.Text = codSc;
                        dataGridView1.SelectedCells[0].Value = Convert.ToDouble(fk.valueEnter.Replace(".", ","));
                    }
                }
                else if (xx == 3)  //pr
                {
                    ov = dataGridView1.Rows[yy].Cells[3].Value;
                    ETALON.FormKolZakaz fk = new ETALON.FormKolZakaz("Введите цену", ov.ToString()); fk.ShowDialog();
                    if (fk.IsSelect)
                    {
                        sql = string.Concat("UPDATE city.sir_td_z SET pr=", fk.valueEnter.ToString().Replace(",", "."), " WHERE cod123= ", o123, ";");
                       // h.m(sql);
                        q.UpdateData(sql);
                        toolStripTextBox1.Text = codSc;
                        dataGridView1.SelectedCells[0].Value = Convert.ToDouble(fk.valueEnter.Replace(".", ","));
                    }
                }
                
            }
        }
        private void cellclick_work()
        {
            //select 
            //name_cod(name,ct) as name, o, p, r, osn, pp, rp, osk, osk-rad+pad as osf, fpa, ct 
            //from ost0;


            //int xx = dataGridView1.SelectedCells[0].ColumnIndex;
            //object y = dataGridView1.SelectedCells[0].Value;
            //int yy = dataGridView1.SelectedCells[0].RowIndex;
            //object obj = dataGridView1.Rows[yy].Cells[0].Value;
            

            string sql = "";
            if (currentOpro == opro.list_doc_month)
            {
                int yy = dataGridView1.SelectedCells[0].RowIndex;
                object obj = dataGridView1.Rows[yy].Cells[0].Value;// H.m(obj);
                codd = obj.ToString();
                object ocodo = dataGridView1.Rows[yy].Cells[2].Value; codo = functionService.nameCod.readCod(ocodo.ToString());
                currentOpro = opro.look_doc;
                load(opro.look_doc);
            }
            else if (currentOpro == opro.r)
            {
                int yy = dataGridView1.SelectedCells[0].RowIndex;
                object obj = dataGridView1.Rows[yy].Cells[0].Value; h.m(obj);
                
                object cto = dataGridView1.Rows[yy].Cells[1].Value; ct = functionService.nameCod.readCod(cto.ToString());
                currentOpro = opro.look_ct_in_doc_r;
                load(currentOpro);
            }
            else if (currentOpro == opro.rp)
            {
                int yy = dataGridView1.SelectedCells[0].RowIndex;
                object obj = dataGridView1.Rows[yy].Cells[0].Value; h.m(obj);

                object cto = dataGridView1.Rows[yy].Cells[1].Value; ct = functionService.nameCod.readCod(cto.ToString());
                currentOpro = opro.look_ct_in_doc_rp;
                load(currentOpro);
            }
        }

        void set()
        {
            if (currentOpro == opro.p || currentOpro == opro.pp)
            {
                #region p-pp
                int ii = 60, jj = 20;
                dataGridView1.Columns[0].Width = 110;
                dataGridView1.Columns[1].Width = 110;
                dataGridView1.Columns[2].Width = 110;
                dataGridView1.Columns[3].Width = 150;
                dataGridView1.Columns[4].Width = 70;
                dataGridView1.Columns[5].Width = 70;
                dataGridView1.Columns[6].Width = 70;
                dataGridView1.Columns[7].Width = 0;

                dataGridView1.Columns[0].HeaderText = "КодДок";
                dataGridView1.Columns[1].HeaderText = "Дата";
                dataGridView1.Columns[2].HeaderText = "ОтКого";
                dataGridView1.Columns[3].HeaderText = "Сырье";
                dataGridView1.Columns[4].HeaderText = "Кол1";
                dataGridView1.Columns[5].HeaderText = "Кол2";
                dataGridView1.Columns[6].HeaderText = "Кол";
                dataGridView1.Columns[7].HeaderText = "кодТ";
                #endregion
            }
            else if (currentOpro ==  opro.list_doc_month)
            {
                #region list_doc_month
                /*codd coddd dd name doc*/
                //dataGridView1.Columns[0].HeaderText = "codd";
                //dataGridView1.Columns[1].HeaderText = "Док";
                //dataGridView1.Columns[2].HeaderText = "Дата";
                //dataGridView1.Columns[3].HeaderText = "Наименование";
                //dataGridView1.Columns[4].HeaderText = "Тип";

                dataGridView1.Columns[0].HeaderText = "Док";
                dataGridView1.Columns[1].HeaderText = "Дата";
                dataGridView1.Columns[2].HeaderText = "Наименование";
                dataGridView1.Columns[3].HeaderText = "Тип";

                dataGridView1.Columns[0].Width = 100;
                dataGridView1.Columns[1].Width = 90;
                dataGridView1.Columns[2].Width = 150;
                dataGridView1.Columns[3].Width = 90;

                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                #endregion
            }
            else if (currentOpro == opro.look_doc )
            {
                #region look_doc
                // H.m(currentOpro);
                dataGridView1.Columns[0].HeaderText = "Док";
                dataGridView1.Columns[1].HeaderText = "Дата";
                dataGridView1.Columns[2].HeaderText = "Наименование";
                dataGridView1.Columns[3].HeaderText = "Тип";
                dataGridView1.Columns[4].HeaderText = "Наименование";
                dataGridView1.Columns[5].HeaderText = "Кол1";
                dataGridView1.Columns[6].HeaderText = "Кол2";
                dataGridView1.Columns[7].HeaderText = "Кол";
                dataGridView1.Columns[8].HeaderText = "Цена";
                dataGridView1.Columns[9].HeaderText = "Сумма";

                dataGridView1.Columns[0].Width = 110;
                dataGridView1.Columns[1].Width = 110;
                dataGridView1.Columns[2].Width = 140;
                dataGridView1.Columns[3].Width = 90;
                dataGridView1.Columns[4].Width = 210;
                dataGridView1.Columns[5].Width = 70;
                dataGridView1.Columns[6].Width = 70;
                dataGridView1.Columns[7].Width = 70;
                dataGridView1.Columns[8].Width = 70;
                dataGridView1.Columns[9].Width = 70;
                

                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                #endregion
            }
            else if (currentOpro == opro.r || currentOpro == opro.rp)
            {
                #region r-rp
                /*select n0-0, n1-1, kr1,
n2-3, k2-4, kv2-5,
n2s-6, kr2-7,
n3-8, k3-9, kv3-10,
n3s-11, kr3-12,
n4-13, k4-14, kv4-15, kr4-16,
city.name_cod(z.name, codts) as n44-17,
kr as rob-18,
codts as ct-19
from t55*/
                string[] aa = new string[]
                {
                "НаимПрод0", "НаимПрод1", "Расход1",
                "Наим2", "Кол2", "Выход2", 
                "Наим2С", "Расход2", 
                "Наим3", "Кол3", "Выход3",
                "Наим3С", "Расход3",
                "Наим4", "Кол4", "Выход4", "Расход4", "НаимСырья", "РасходСырья", "КодСырья"
                };
                for (int i = 0; i < 20; i++)
                {
                    if (i == 0 || i == 1 || i == 3 || i == 6 || i == 8 || i == 11 || i == 13 || i == 17)
                    { dataGridView1.Columns[i].Width = 100; }
                    else
                    { dataGridView1.Columns[i].Width = 60; dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; }
                    dataGridView1.Columns[i].HeaderText = aa[i];
                }
                #endregion
            }
            else if (currentOpro == opro.look_doc_rp)
            {
                #region look_doc_rp
                /*codd dd codo name kol kol2 k*/
                string[] aa = new string[]
                {
                "КодДок", "Дата", 
                "Наименование", "Продукция",
                "Кол1", "Кол2", "Кол"
                };
                for (int i = 0; i < 7; i++)
                {
                    if (i < 2 )
                    { dataGridView1.Columns[i].Width = 120; }
                    else if (i == 2)
                    { dataGridView1.Columns[i].Width = 150; }
                    else if (i == 3)
                    { dataGridView1.Columns[i].Width = 310; }
                    else
                    { 
                        dataGridView1.Columns[i].Width = 70; 
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    }
                    dataGridView1.Columns[i].HeaderText = aa[i];
                }
                this.WindowState = FormWindowState.Maximized;

                #endregion
            }
            else if (currentOpro == opro.lookOstN900 || currentOpro == opro.lookAnyDoc )
            {
                #region lookOstN900
                /*SELECT name_cod(
    n.name
    , ct) as name, kol, kol2, pr, kol*kol2*pr as sd ,codd, s.cod123, ct
FROM sir_td_z s
left join city.',sTableName,' n on s.ct= n.cod
where codd = ', coddo ,' order by s.cod123 desc;*/
                string[] aa = new string[]
                {
                "Наименование", "Кол1","Кол2", "Цена", "Сумма", "КодД", "№пп", "КодТ"
                };
                for (int i = 0; i < aa.Length; i++)
                {
                    if (i ==0)
                    { 
                        dataGridView1.Columns[i].Width = 260; 
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft ;
                    }
                    else if (i == 1 || i == 2)
                    { dataGridView1.Columns[i].Width = 80;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        dataGridView1.Columns[i].DefaultCellStyle.Format = "F1";
                    }
                    else if (i == 3)
                    {
                        dataGridView1.Columns[i].Width = 90;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        dataGridView1.Columns[i].DefaultCellStyle.Format = "F5";
                    }
                    else if (i == 4)
                    { dataGridView1.Columns[i].Width = 90; 
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        dataGridView1.Columns[i].DefaultCellStyle.Format = "F2";
                    }
                    else if (i == 5)
                    {
                        dataGridView1.Columns[i].Width = 100;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        dataGridView1.Columns[i].DefaultCellStyle.Format = "F0";
                    }
                    else if (i == 6)
                    {
                        dataGridView1.Columns[i].Width = 70;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        dataGridView1.Columns[i].DefaultCellStyle.Format = "F0";
                    }
                    else if (i == 7)
                    {
                        dataGridView1.Columns[i].Width = 70;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        dataGridView1.Columns[i].DefaultCellStyle.Format = "F0";
                    }

                    dataGridView1.Columns[i].HeaderText = aa[i];
                }
                this.WindowState = FormWindowState.Maximized;

                #endregion
            }
            else if (currentOpro == opro.lookAvgPr)
            {
                #region look_doc_rp
                /*nd,dd,vd,corg,ct,pr,k,t,ep,prgr,codts,name, tt,ep,prgr,codts*/
                string[] aa = new string[]
                {
                "Документ", "Дата","ВидД", "КодО", "КодТ",  
                "Цена", "Кол", "Наименование", "Ед", "ЦенаЕд", "КодТС", "Наименование"
                };
                for (int i = 0; i < aa.Length ; i++)
                {
                    if (i < 2)
                    { dataGridView1.Columns[i].Width = 90; dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; }
                    else if (i == 2 || i==3 || i==4)
                    { dataGridView1.Columns[i].Width = 50; dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; }
                    else if (i == 5)
                    { 
                        dataGridView1.Columns[i].Width = 120;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        dataGridView1.Columns[i].DefaultCellStyle.Format = "F4";
                    }
                    else if (i == 6)
                    { dataGridView1.Columns[i].Width = 80; dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; }
                    else if (i == 7)
                    { dataGridView1.Columns[i].Width = 120; }
                    else if (i == 8)
                    { dataGridView1.Columns[i].Width = 50; dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; }
                    else if (i == 9)
                    {
                        dataGridView1.Columns[i].Width = 90; dataGridView1.Columns[i].DefaultCellStyle.Format = "F5";
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; }
                    else if (i == 10)
                    { dataGridView1.Columns[i].Width = 70; dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; }
                    else if (i == 11)
                    { dataGridView1.Columns[i].Width = 120; }
                    
                    dataGridView1.Columns[i].HeaderText = aa[i];
                }
                this.WindowState = FormWindowState.Maximized;

                #endregion
            }
            else if (currentOpro == opro.look_All_rezpt || currentOpro == opro.look_All_rezpt_ct
                || currentOpro == opro.look_All_rezptBPeriod  || currentOpro == opro.look_All_rezpt_BPeriod_ct
                || currentOpro == opro.look_SirPer  || currentOpro == opro.look_SirPer_Before )
            {
                #region look_All_rezpt
                /*n0 n1 kr1 
                 * n2 k2 kv2 
                 * n2s kr2 n3      k3 kv3 n3s     kr3 n4 k4 kv4 kr4 n44 rob ct
                "Наименование0" 0, "Наименование1" 1, "КолР1" 2,
                "Наименование2" 3, "Кол2" 4, "КолВых2" 5,
                "Наименование2с" 6, "КолР2" 7, "Наименование3" 8, "Кол3" 9, КолВ3-10 "Наименование3с" 11, "КолР3" 12,
                "Наименование4" 13, "Кол4" 14, "КолВ4" 15, "КолР4" 16, "Наименование" 17, "КолРасх" 18, "КодТ" 19
                 */
                string[] aa = new string[]
                {
                "Наименование0", "Наименование1", "КолР1",
                "Наименование2", "Кол2", "КолВых2",
                "Наименование2с", "КолР2", "Наименование3", "Кол3", "КолВ3", "Наименование3с", "КолР3",
                "Наименование4", "Кол4", "КолВ4", "КолР4", "Наименование", "КолРасх", "КодТ"
                };
                for (int i = 0; i < 20; i++)
                {
                    if (i == 3 || i == 6 || i == 8 || i == 11 || i == 13 || i == 17)
                    { dataGridView1.Columns[i].Width = 180; }
                    else if (i == 0 || i == 1)
                    { dataGridView1.Columns[i].Width = 240; }
                    else if (i == 19)
                    { dataGridView1.Columns[i].Width = 50; }
                    else if (i == 12 || i == 16 || i == 18)
                    { dataGridView1.Columns[i].Width = 70;
                    dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    dataGridView1.Columns[i].DefaultCellStyle.Format = "F1";
                    }
                    else if (i == 2 )
                    {
                        dataGridView1.Columns[i].Width = 90;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        dataGridView1.Columns[i].DefaultCellStyle.Format = "F1";
                    }
                    else
                    { dataGridView1.Columns[i].Width = 70;
                    dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    dataGridView1.Columns[i].DefaultCellStyle.Format = "F1";
                    }
                    dataGridView1.Columns[i].HeaderText = aa[i];
                }
                this.WindowState = FormWindowState.Maximized;

                #endregion
            }
            else if (currentOpro == opro.lookDoc_PRIHOD || currentOpro == opro.lookDoc_RASHOD || currentOpro == opro.lookDoc_RASHODnr)
            {
                #region lookDoc_PRIHOD lookDoc_PRIHOD
                /*codd dd ct kol kol2 k pr sd
                "КодДок" 0, "Дата" 1, "КодТ" 2,
                "Кол1" 3, "Кол2" 4, "Кол" 5,
                "Цена" 6, "Сумма" 7
                 */
                string[] aa = new string[]
                {
                "КодДок", "Дата", "КодО", "КодТ",
                "Кол1", "Кол2", "Кол",
                "Цена", "Сумма"
                };
                for (int i = 0; i < aa.Length; i++)
                {
                    if (i == 0 || i == 1 ||  i == 8 ||i == 9)
                    { dataGridView1.Columns[i].Width = 100;
                    dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

                    }
                    else if (i == 7)
                    { dataGridView1.Columns[i].Width = 100; dataGridView1.Columns[i].DefaultCellStyle.Format = "F5"; }
                    else if (i == 2 || i == 3)
                    { dataGridView1.Columns[i].Width = 180; }
                    else if (i == 4 || i == 5 || i == 6)
                    { dataGridView1.Columns[i].Width = 90;
                    dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    dataGridView1.Columns[i].DefaultCellStyle.Format = "F1";
                    }

                    dataGridView1.Columns[i].HeaderText = aa[i];
                }
                this.WindowState = FormWindowState.Maximized;

                #endregion
            }
            else if (currentOpro == opro.lookDoc_RASHODdays)
            {
                #region lookDoc_RASHODdays
                /*codd dd ct kol kol2 k pr sd
                "КодДок" 0, "Дата" 1, "КодТ" 2,
                "Кол1" 3, "Кол2" 4, "Кол" 5,
                "Цена" 6, "Сумма" 7
                 */
                string[] aa = new string[]
                {
                 "НаименованиеПрод", "Дата", "Кол",
                };
                for (int i = 0; i < aa.Length; i++)
                {
                    if (i == 0 )
                    {
                        dataGridView1.Columns[i].Width = 280;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft  ;
                    }
                    else if (i == 1)
                    {
                        dataGridView1.Columns[i].Width = 90;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    }
                    else if (i == 2)
                    {
                        dataGridView1.Columns[i].Width = 90;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        dataGridView1.Columns[i].DefaultCellStyle.Format = "F1";
                    }
                    dataGridView1.Columns[i].HeaderText = aa[i];
                }
                this.WindowState = FormWindowState.Maximized;

                #endregion
            }
            else if (currentOpro == opro.look_r4_rdtd_prod)
            {
                #region look_r4_rdtd_prod
                
                string[] aa = new string[]
                {
                 "НаименованиеПрод", "Вес", 
                 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                 "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
                 "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
                 "Шт", "ИтВес"
                };
                for (int i = 0; i < aa.Length; i++)
                {
                    if (i == 0)
                    {
                        dataGridView1.Columns[i].Width = 280;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                    }
                    else if (i == 1)
                    {
                        dataGridView1.Columns[i].Width = 50;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    }
                    else if (i>1 && i<33)
                    {
                        dataGridView1.Columns[i].Width = 60;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        dataGridView1.Columns[i].DefaultCellStyle.Format = "F0";
                    }
                    else if (i>32)
                    {
                        dataGridView1.Columns[i].Width = 80;
                        dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        dataGridView1.Columns[i].DefaultCellStyle.Format = "F0";
                    }
                    dataGridView1.Columns[i].HeaderText = aa[i];
                }
                this.WindowState = FormWindowState.Maximized;
                
                #endregion
            }
            else if (currentOpro == opro.look_ost_workDay_s2_1__param2)
            {
                #region look_ost_workDay_s2_1__param2
                /*create  table citytemp.owd_ost_wd_withoutbefore
select name_cod(name,ct) as name, round(osn,2) as ostn, pp, rp, round(osk,2) as ostk,
round(osk-rad+pad,2) as osf, round(osk - (osk-rad+pad),2)  as otkl
 from citytemp.owd_ost0*/
                string[] aa = new string[]
                {
                 "НаименованиеПрод", "ОстН", 
                 "Прих", "Расх", "ОстК", "ОстФ", "Откл"
                };
                if (dataGridView1.RowCount > 0)
                {
                    for (int i = 0; i < aa.Length; i++)
                    {
                        if (i == 0)
                        {
                            dataGridView1.Columns[i].Width = 280;
                            dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                        }
                        else
                        {
                            dataGridView1.Columns[i].Width = 100;
                            dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                            dataGridView1.Columns[i].DefaultCellStyle.Format = "F0";
                        }
                        dataGridView1.Columns[i].HeaderText = aa[i];
                    }
                }
                this.WindowState = FormWindowState.Maximized;

                #endregion
            }
        }
        void setMode(rzpt.modeWAED mo)
        {
            currentMode = mo;
            if (mo == city7.rzpt.modeWAED.work)
            {
                setColor(Color.LightGray, Color.White);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
                // refresh();
            }
            else if (mo == city7.rzpt.modeWAED.add)
            {

            }
            else if (mo == city7.rzpt.modeWAED.edit)
            {
                setColor(Color.LightGreen, Color.LightSkyBlue);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            }
            else if (mo == city7.rzpt.modeWAED.delete)
            {

            }
            else if (mo == city7.rzpt.modeWAED.non)
            {

            }

        }
        void setColor(Color c1, Color c2)
        {
            if (currentOpro == opro.p || currentOpro == opro.pp)
            {
                #region p-pp
                dataGridView1.Columns[0].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[1].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[2].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[3].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[4].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[5].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[6].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[7].DefaultCellStyle.BackColor = c1;
                #endregion
            }
            else if (currentOpro == opro.lookOstN900 || currentOpro == opro.lookAnyDoc )
            {
                #region list_doc_month
                /*SELECT name_cod(
    n.name
    , ct) as name, kol, kol2, pr, kol*kol2*pr as sd ,codd, s.cod123, ct
FROM sir_td_z s
left join city.',sTableName,' n on s.ct= n.cod
where codd = ', coddo ,' order by s.cod123 desc;*/
                dataGridView1.Columns[0].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[1].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[2].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[3].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[4].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[5].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[6].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[7].DefaultCellStyle.BackColor = c1;
                // dataGridView1.Columns[4].DefaultCellStyle.BackColor = c1;
                #endregion
            }
            else if (currentOpro == opro.list_doc_month)
            {
                #region list_doc_month
                /*codd coddd dd name doc*/
                dataGridView1.Columns[0].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[1].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[2].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[3].DefaultCellStyle.BackColor = c1;
                // dataGridView1.Columns[4].DefaultCellStyle.BackColor = c1;
                #endregion
            }
            else if (currentOpro == opro.look_doc)
            {
                #region look_doc
                /*codd coddd dd name doc*/
                dataGridView1.Columns[0].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[1].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[2].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[3].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[4].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[5].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[6].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[7].DefaultCellStyle.BackColor = c2;
                dataGridView1.Columns[8].DefaultCellStyle.BackColor = c1;
                dataGridView1.Columns[9].DefaultCellStyle.BackColor = c1;
                // dataGridView1.Columns[4].DefaultCellStyle.BackColor = c1;
                #endregion
            }
            else if (currentOpro == opro.look_ct_in_doc_r || currentOpro == opro.look_ct_in_doc_rp)
            {
                #region look_ct_in_doc_rp  look_ct_in_doc_r
                /*select rd.*, name_cod( z.name,ct) as ct,  kol, kol2,kol*kol2 as k, pr, kol*kol2*pr as sd, t.cod123 from rd
left join sir_td_z t on rd.codd=t.codd
left join list_sc z on t.ct = z.cod
where t.ct = ct0
order by z.name
                 * 
                 * 
                 * codd dd name doc ct kol kol2 k pr sd cod123
                 */
                //dataGridView1.Columns[0].DefaultCellStyle.BackColor = c1;
                //dataGridView1.Columns[1].DefaultCellStyle.BackColor = c1;
                //dataGridView1.Columns[2].DefaultCellStyle.BackColor = c2;
                //dataGridView1.Columns[3].DefaultCellStyle.BackColor = c1;
                //dataGridView1.Columns[4].DefaultCellStyle.BackColor = c2;
                //dataGridView1.Columns[5].DefaultCellStyle.BackColor = c1;
                //dataGridView1.Columns[6].DefaultCellStyle.BackColor = c1;
                //dataGridView1.Columns[7].DefaultCellStyle.BackColor = c2;
                //dataGridView1.Columns[8].DefaultCellStyle.BackColor = c1;
                //dataGridView1.Columns[9].DefaultCellStyle.BackColor = c1;
                // dataGridView1.Columns[4].DefaultCellStyle.BackColor = c1;
                #endregion
            }
            else if (currentOpro == opro.r || currentOpro == opro.rp)
            {
                #region r-rp
                /*select n0-0, n1-1, kr1,
n2-3, k2-4, kv2-5,
n2s-6, kr2-7,
n3-8, k3-9, kv3-10,
n3s-11, kr3-12,
n4-13, k4-14, kv4-15, kr4-16,
city.name_cod(z.name, codts) as n44-17,
kr as rob-18,
codts as ct-19
from t55*/
                for (int i = 0; i < 20; i++)
                {
                    if (i == 0 || i == 1 || i == 3 || i == 6 || i == 8 || i == 11 || i == 13 || i == 17)
                        dataGridView1.Columns[i].DefaultCellStyle.BackColor = c1;
                    else
                        dataGridView1.Columns[i].DefaultCellStyle.BackColor = c2;
                }
                #endregion
            }
            else if (currentOpro == opro.look_doc_rp)
            {

            }
            else if (currentOpro == opro.look_r4_rdtd_prod)
            {
                for (int i = 0; i < 35; i++)
                {
                    if (i == 0 || i == 1 || i == 33 || i == 34 )
                        dataGridView1.Columns[i].DefaultCellStyle.BackColor = c1;
                    else
                        dataGridView1.Columns[i].DefaultCellStyle.BackColor = c2;
                }
            }
            else if (currentOpro == opro.look_ost_workDay_s2_1__param2 )
            {
                for (int i = 0; i < 6; i++)
                {
                    if (i == 1 || i == 4)
                        dataGridView1.Columns[i].DefaultCellStyle.BackColor = c1;
                    else
                        dataGridView1.Columns[i].DefaultCellStyle.BackColor = c2;
                }
            }
        }
        void load(opro opro0)
        {

            string sql = ""; object ss = "";
            if (opro0 == opro.p) //- приход до пер
            {
                #region p
                sql = string.Concat("call `city`.`ost_prih_beforeWorkDay_viewDoc` (",
                    sm, ", ", ds, ",", ct, ", 1);"); //H.m(sql);
                dts = q.selectAny(sql); dataGridView1.DataSource = dts;
                #endregion
            }
            else if (opro0 == opro.r)//- расход до пер
            {
                #region r
                #endregion
            }
            else if (opro0 == opro.look_ct_in_doc_r)
            {
                
            }
            else if (opro0 == opro.look_ct_in_doc_rp)
            {
                #region look_ct_in_doc_rp
                string s = q.tk(ETALON.param.period_s_5), p = q.tk(ETALON.param.period_p_6);

                int ps = da.codDATE_10913_from_DATETIME(q.tk(ETALON.param.period_s_5));
                int pp = da.codDATE_10913_from_DATETIME(q.tk(ETALON.param.period_p_6));
                sql = string.Concat("call `city`.`look_doc_tov_ct` (",
                  ps, ", ", pp, ", 4, 2,", ct, ");");// h.m(sql);
                dts = q.selectAny(sql); dataGridView1.DataSource = dts;
                #endregion
            }
            else if (opro0 == opro.lookAvgPr)
            {
                #region lookAvgPr
                sql = string.Concat("call `city`.`r4_prih_avgPr` (", sm, ",' ',0);");// h.m(sql);
                dts = q.selectAny(sql); dataGridView1.DataSource = dts;// q.sh(dts);
                #endregion
            }
            else if (opro0 == opro.pp)//- приход за период
            {
                #region pp
                sql = string.Concat("call `city`.`ost_prih_WorkDay_viewDoc_ct` (",
                   sm, ", ", ds, ", ", dp, ",", ct, ", 1);"); //H.m(sql);
                #endregion
            }
            else if (opro0 == opro.rp)//- расход за период
            {
               
            }
            else if (opro0 == opro.lookOstN900)//- начальные ост
            {
                #region lookOstN900
                sql = string.Concat("call `sir_td_z900`(",sm,"011900, 'list_sc');");
                dts = q.selectAny(sql); dataGridView1.DataSource = dts;
                toolStripButton6.Enabled = true;
                #endregion
            }
            else if (opro0 == opro.lookAnyDoc )//- текущий документ
            {
                #region lookAnyDoc
                object iCodd = q.tk_readTagRd(ETALON.param.sir_tag_rd_24, ETALON.raw.typeTagRd.codd_5);
                sql = string.Concat("call `sir_td_z900`(", iCodd, ", 'list_sc');");
                dts = q.selectAny(sql); dataGridView1.DataSource = dts;
                toolStripButton6.Enabled = true;
                #endregion
            }
            else if (opro0 == opro.ofk)//- ост факт
            { }
            else if (opro0 == opro.list_doc_month)//- расход до пер
            {
                #region list_doc_month
                sql = string.Concat("call `city`.`look_doc_month` (", sm, ");"); //H.m(sql);
                dts = q.selectAny(sql); dataGridView1.DataSource = dts;
                #endregion
            }
            else if (opro0 == opro.lookDoc_PRIHOD)
            {
                #region lookDoc_PRIHOD
                sql = string.Concat("call `city`.`r4_rdtd_prihod` (", sm, ", 888, ", q.OKNO , ");"); h.m(sql);
                dts = q.selectAny(sql); //q.sh(dts);
                dataGridView1.DataSource = dts;
                #endregion
            }
            else if (opro0 == opro.lookDoc_RASHODdays)
            {
                #region lookDoc_RASHODdays
                sql = string.Concat("call `city`.`r4_rdtd_prod` (",
                                  sm, ",", dateB, ",", dateE, ", 888, 8,", q.OKNO , ");");
                //sql = string.Concat("call `city`.`r4_rdtd_prod` (",
                //                     sm, ", 888, 8,1);"); //H.m(sql);
                dts = q.selectAny(sql);// q.sh(dts);
                    //ss = dts.Compute("sum(sd)", string.Concat("codd>0")); lable_ss.Text = ss.ToString();
                    dataGridView1.DataSource = dts;
                #endregion
            }
            else if (opro0 == opro.look_r4_rdtd_prod )
            {
                #region look_r4_rdtd_prod
                sql = string.Concat("call `city`.`r4_rdtd_prod` (",
                                     sm, ",", dateB, ",", dateE, ", 888, 7,", q.OKNO, ");");
                dts = q.selectAny(sql); // q.sh(dts);
                dataGridView1.DataSource = dts;
                #endregion
            }
            else if (opro0 == opro.lookDoc_RASHOD)
            {
                #region lookDoc_RASHOD
                sql = string.Concat("call `city`.`r4_rdtd_prod` (",
                                     sm, ",", dateB, ",", dateE, ", 888, 1,", q.OKNO, ");");// h.m(sql);
                //sql = string.Concat("call `city`.`r4_rdtd_prod` (",
                //                     sm, ", 888, 1,", ich, ");"); 
                dts = q.selectAny(sql);// q.sh(dts);
                //ss = dts.Compute("sum(sd)", string.Concat("codd>0")); lable_ss.Text = ss.ToString();
                dataGridView1.DataSource = dts;
                #endregion
            }
            else if (opro0 == opro.lookDoc_RASHODnr)
            {
                #region lookDoc_RASHODnr
                sql = string.Concat("call `city`.`r4_rdtd_prod_4codo` (",
                                     sm, ", 888);"); //H.m(sql);
                dts = q.selectAny(sql);// q.sh(dts);
                //ss = dts.Compute("sum(sd)", string.Concat("codd>0")); lable_ss.Text = ss.ToString();
                dataGridView1.DataSource = dts;
                #endregion
            }
            else if (opro0 == opro.look_SirPer_Before )
            {
                #region look_SirPer_Before
                // CALL city.look_ras_td_203beforePeriod(203,
//1, 28, 888,   14);
               
                if (MessageBox.Show("Пересчитать расход сырья до начала периода", "Выбор", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    sql = string.Concat("call `city`.`look_ras_td_203beforePeriod` (",
                                    sm, ", ", ds, ", ", dp, ", 888, 14);"); //H.m(sql);
                }
                else
                {
                    sql = string.Concat("SELECT * FROM ost_rashSir_WorkDay_beforePeriod;");
                }
                
                
                dts = q.selectAny(sql);// q.sh(dts);
                dataGridView1.DataSource = dts;
                #endregion
            }
            else if (opro0 == opro.look_SirPer)
            {
                #region look_SirPer
                //call `city`.`look_ras_td_203`(203,
//1, 28, 888,   14);
                // codd dd ct kol kol2 k pr sd

                if (MessageBox.Show("Пересчитать расход сырья за период", "Выбор", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    sql = string.Concat("call `city`.`look_ras_td_203` (",
                                         sm, ", ", ds, ", ", dp, ", 888, 14);"); //H.m(sql);
                }
                else
                {
                    sql = string.Concat("SELECT * FROM ost_rashSir_WorkDay;");
                }
                dts = q.selectAny(sql); //q.sh(dts);
                dataGridView1.DataSource = dts;
                #endregion
            }
            else if (opro0 == opro.look_ost_workDay_s2_1__param2 )
            {
                #region look_ost_workDay_s2_1__param2
                //call `city`.`look_ras_td_203`(203,
                //1, 28, 888,   14);
                // codd dd ct kol kol2 k pr sd

            //     int ii=0;
            //if (MessageBox.Show("Пересчитать остатки", "Выбор", 
            //    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ii = 1;  } else {  ii=0; }

            string sql0 = string.Concat("call `city`.`ost_workDay_s2_1` (", sm, ", ", ds, ",", dp, ", 2, ", q.OKNO , ");"); //h.m(sql0 );
            dts = q.selectAny(sql0); //q.sh(dts);
              dataGridView1.DataSource = dts;
                #endregion
            }
            else if (opro0 == opro.look_doc )//- расход до пер
            {
                #region look_doc
                /*
                codd
                dd
                name
                doc
                ct
                kol
                kol2
                k
                pr
                sd
                call `city`.`look_doc_tov` (203011900,1);
                 */
                
                sql = string.Concat("call `city`.`look_doc_tov` (", codd, ",", codd.Substring(5,1) ,",", codo ,");"); 
                dts = q.selectAny(sql); //q.sh(dts);
                
                dataGridView1.DataSource = dts;
                set(); 
                setColor(Color.LightGray, Color.White);
                #endregion
            }
           
        }
        void load(opro opro0, object ct0)
        {
            string sql = ""; object ss = 0;

            if (opro0 == opro.p) //- приход до пер
            {
                #region приход до пер
                /*`ost_prih_beforeWorkDay_viewDoc_ct`(
    year_monthCurrent_203 int,
    dayB int, ct0 int,
    LOOK_1_DONTLOOK_0 INT)*/
                dts = q.selectAny(string.Concat("call `city`.`ost_prih_beforeWorkDay_viewDoc_ct` (",
                    sm, ", ", ds, ",", ct0, ", 1);")); // q.sh(dts);
                ss = dts.Compute("sum(k)", "codd>0"); //H.m(H.aa("sum(k) ", "codd>0 ",ss));
                lable_ss.Text =  h.fo(ss.ToString());
                /*select t.codd, dop.dd, name_cod(o.name,dop.codo) as org,
name_cod(z.name, t.ct) as name, t.kol,t.kol2, t.kol*t.kol2 as k,
t.ct
from sir_td_z t*/
                #endregion
            }
            else if (opro0 == opro.r)//- расход до пер
            {
                #region расход до пер
                sql = string.Concat("call `city`.`nostPrihRashOst_rashod2_ct` (",
                         sm, ", ", ds, ", ", ct, ", 1);"); //H.m(sql);
                dts = q.selectAny(sql); //q.sh(dts);
                ss = dts.Compute("sum(rob)", "ct>0"); lable_ss.Text = h.fo(ss.ToString());
                //lable_ss.Text = Convert.ToDouble(ss != null ? ss : 0).ToString();
                #endregion
            }
            else if (opro0 == opro.pp)//- приход за период
            {
                #region приход за период
                /*`ost_prih_WorkDay_viewDoc_ct`(year_monthCurrent_203 int,
    dayB int, dayE int, ct0 int,
    LOOK_1_DONTLOOK_0 INT)*/
                sql = string.Concat("call `city`.`ost_prih_WorkDay_viewDoc_ct` (",
                  sm, ", ", ds, ", ", dp, ",", ct, ", 1);"); //H.m(sql);

                dts = q.selectAny(sql); 
                //q.sh(dts);
                ss = dts.Compute("sum(k)", "codd>0"); lable_ss.Text = h.fo(ss.ToString());
                //lable_ss.Text = Convert.ToDouble(ss != null ? ss : 0).ToString();

                /*select t.codd, dop.dd, name_cod(o.name,dop.codo) as org, name_cod(z.name, t.ct) as name,
t.kol,t.kol2, t.kol*t.kol2 as k, t.ct
from sir_td_z*/
                #endregion
            }
            else if (opro0 == opro.rp)//- расход за период
            {
                #region расход за период
                sql = string.Concat("call `city`.`look_ras_td_203` (",
                       sm, ", ", ds, ", ", dp, ",", ct, ", 1);"); //H.m(sql);
                dts = q.selectAny(sql); //q.sh(dts);
                ss = dts.Compute("sum(rob)", "ct>0"); lable_ss.Text = h.fo(ss.ToString());
                #endregion
            }
            else if (opro0 == opro.ofk)//- ост факт
            { }
            else if (opro0 == opro.look_doc_rp)//- rasho za period
            {
                #region  look_doc_rp
                //call `city`.`look_ras_td_203`(203,1,1, 888,11);
                //call `city`.`look_ras_td_203`(203,1,1, 888,11);
                sql = string.Concat("call `city`.`look_ras_td_203` (",
                                     sm, ", ", ds, ", ", dp, ",888, 11);");// H.m(sql);
                // call `city`.`look_ras_td_203`(203,1,1, 888,1);
                dts = q.selectAny(sql);
                #endregion
            }
            else if (opro0 == opro.look_doc_r)//- ост факт
            {
                #region  look_doc_r
                //call `city`.`look_ras_td_203`(203,1,1, 888,11);
                //call `city`.`look_ras_td_203`(203,1,1, 888,11);
                sql = string.Concat("call `city`.`look_ras_td_203beforePeriod` (",
                                     sm, ", 1, ", ds1, ",888, 11);");// H.m(sql);
                // call `city`.`look_ras_td_203`(203,1,1, 888,1);
                dts = q.selectAny(sql);//q.sh(dts);
                #endregion
            }
            else if (opro0 == opro.look_All_rezptBPeriod)//- ост факт
            {
                #region  look_All_rezptBPeriod
                sql = string.Concat("call `city`.`look_ras_td_203beforePeriod` (",
                                     sm, ", ", ds, ", ", dp, ",888, 14);"); //H.m(sql);
                // call `city`.`look_ras_td_203`(203,1,1, 888,1);
                dts = q.selectAny(sql); //q.sh(dts);
                ss = dts.Compute("sum(rob)", "ct>0"); lable_ss.Text = h.fo(ss.ToString());
                #endregion
            }
            else if (opro0 == opro.look_All_rezpt)//- ост факт
            {
                #region  look_All_rezpt
                //call `city`.`look_ras_td_203`(203,1,1, 888,11);
                sql = string.Concat("call `city`.`look_ras_td_203` (",
                                     sm, ", ", ds, ", ", dp, ",888, 14);"); //H.m(sql);
                // call `city`.`look_ras_td_203`(203,1,1, 888,1);
                dts = q.selectAny(sql); //q.sh(dts);
                ss = dts.Compute("sum(rob)", "ct>0"); lable_ss.Text = h.fo(ss.ToString());
                #endregion
            }
            else if (opro0 == opro.look_All_rezpt_BPeriod_ct)//- ост факт
            {
                #region  look_All_rezpt_BPeriod_ct
                sql = string.Concat("call `city`.`look_ras_td_203beforePeriod` (",
                                     sm, ", ", ds, ", ", dp, ",", ct, ", 1);"); //H.m(sql);
                dts = q.selectAny(sql); //q.sh(dts);
                ss = dts.Compute("sum(rob)", string.Concat("ct=", ct)); lable_ss.Text = h.fo(ss.ToString());
            #endregion
            }
            else if (opro0 == opro.look_All_rezpt_ct)//- ост факт
            {
                #region  look_All_rezpt_ct
                sql = string.Concat("call `city`.`look_ras_td_203` (",
                                     sm, ", ", ds, ", ", dp, ",", ct, ", 1);"); //H.m(sql);
                dts = q.selectAny(sql); //q.sh(dts);
                ss = dts.Compute("sum(rob)", string.Concat("ct=", ct)); lable_ss.Text = h.fo(ss.ToString());
            #endregion
            }
            else if (opro0 == opro.lookDoc_PRIHOD)
            {
                #region  lookDoc_PRIHOD
                //call `city`.`skl_rdtd`(203,978); codd dd ct kol kol2 k pr sd
                sql = string.Concat("call `city`.`skl_rdtd` (",
                                     sm, ", 888);"); //H.m(sql);
                dts = q.selectAny(sql); //q.sh(dts);
                ss = dts.Compute("sum(sd)", string.Concat("codd>0")); lable_ss.Text = h.fo(ss.ToString());
            #endregion
            }
            
            dataGridView1.DataSource = dts;
        }
        object dateB { get {return  q.tk_int_day_fromDate(ETALON.param.period_s_5); } }
        object dateE { get {return  q.tk_int_day_fromDate(ETALON.param.period_p_6); } }
        public void editParam_SELECT_sirTd(int cod123o)
        {
                        //            //r[0]-name, r[1]-kol, r[2]-kol2 , r[3]-pr, r[5]-codd, r[6]-cod123 
                        //            string tagSelect = "";
                        //            /*
                        //name
                        //kol
                        //kol2
                        //pr
                        //--sd
                        //codd
                        //cod123
                                     
                                     
                                     
                        //             ТОРТ фруктовый ПЕРСИК (35);1;690;0;203031001;3304  */

                        //            ListViewItem[] vi = new ListViewItem[6];
                        //            #region name-index-tag
                        //            vi[0] = new ListViewItem("Наименование");
                        //            vi[1] = new ListViewItem("Кол");
                        //            vi[2] = new ListViewItem("Кол2");
                        //            vi[3] = new ListViewItem("Цена");
                        //            vi[4] = new ListViewItem("КодД");
                        //            vi[5] = new ListViewItem("Код123");

                        //            vi[0].ImageIndex = 1;
                        //            vi[1].ImageIndex = 3;
                        //            vi[2].ImageIndex = 3;
                        //            vi[3].ImageIndex = 3;
                        //            vi[4].ImageIndex = 3;
                        //            vi[5].ImageIndex = 3;

                        //            vi[0].Tag = string.Concat((int)ETALON.workType.selectProdZeh, "; name");
                        //            vi[1].Tag = string.Concat((int)ETALON.workType.selectKol_siTd, "; kol");
                        //            vi[2].Tag = string.Concat((int)ETALON.workType.selectKol2_sirTd, "; kol2");
                        //            vi[3].Tag = string.Concat((int)ETALON.workType.selectPr_sirTd, "; pr");
                        //            vi[4].Tag = string.Concat((int)ETALON.workType.non, "; codd");
                        //            vi[5].Tag = string.Concat((int)ETALON.workType.non, "; cod123");

                        //            #endregion

                        //            //functionService.tagWork.readTAG 
                        //            //vi[0].SubItems.Add(q.tk_readTagRd(param.sir_tag_rd_24, ETALON.raw.typeTagRd.nd_1));
                        //            vv.editTd.name_cod = q.tk_readTagTd(param.sir_td_tag_29, ETALON.raw.typeTagTd.name_0);
                        //            vv.editTd.ct = nc.readCod(vv.editTd.name_cod);
                        //            vv.editTd.currentCod123 = q.tk_readTagTd(ETALON.param.sir_td_tag_29, ETALON.raw.typeTagTd.cod123_5);
                        //            vv.editTd.kol = q.tk_readTagTd(ETALON.param.sir_td_tag_29, ETALON.raw.typeTagTd.kol_1);
                        //            vv.editTd.kol2 = q.tk_readTagTd(ETALON.param.sir_td_tag_29, ETALON.raw.typeTagTd.kol2_2);
                        //            vv.editTd.pr = q.tk_readTagTd(ETALON.param.sir_td_tag_29, ETALON.raw.typeTagTd.pr_3);
                        //            vv.editTd.codd = q.tk_readTagTd(ETALON.param.sir_td_tag_29, ETALON.raw.typeTagTd.codd_4);

                        //            vi[0].SubItems.Add(vv.editTd.name_cod);
                        //            vi[1].SubItems.Add(vv.editTd.kol);
                        //            vi[2].SubItems.Add(vv.editTd.kol2);
                        //            vi[3].SubItems.Add(vv.editTd.pr);
                        //            vi[4].SubItems.Add(vv.editTd.codd);
                        //            vi[5].SubItems.Add(vv.editTd.currentCod123);

                        //            string s24 = q.tk(param.sir_td_tag_29);

                        //            vi[0].Font = new Font("Tahoma", 24);
                        //            vi[1].Font = new Font("Tahoma", 24);
                        //            vi[2].Font = new Font("Tahoma", 24);
                        //            vi[3].Font = new Font("Tahoma", 24);
                        //            vi[4].Font = new Font("Tahoma", 24);
                        //            vi[5].Font = new Font("Tahoma", 24);

                        //            FormListParam f = new FormListParam(vi); f.ShowDialog();

                        //            if (f.IsOk)
                        //            {
                        //                string s1 = f.listView1.Items[0].SubItems[1].Text;
                        //                /*
                        //name
                        //kol
                        //kol2
                        //pr
                        //--sd
                        //codd
                        //cod123*/
                        //                string sql_Update = string.Concat("UPDATE ", shema, ".", sirTdZ,
                        //                    " SET ct = ", vv.editTd.ct,
                        //                    ", kol = ", vv.editTd.kol, //da.date_to_cod_str( vv.editParam_date_25)
                        //                    ", kol2 = ", vv.editTd.kol2,
                        //                    ", pr = ", vv.editTd.pr.Replace(",", "."),
                        //                    " where cod123 = ", cod123o, ";");

                        //                //if (vv.o_l_0_cod == 4) h.m(h.az(sql_Update, " only for programmer"));

                        //                q.UpdateData(sql_Update);
                        //                // object o = q.selectScalar(string.Concat("SELECT listSourse FROM sir_listorg WHERE cod=", vv.sir_org_23_CodInt, ";"));
                        //                //vv.list_sourse = o.ToString();
                        //                loadDocTd();
                        //            }
        }

       

        private void toolStripDropDownButton3_Click(object sender, EventArgs e)
        {
            //currentOpro = opro.list_doc_month;
            //load(opro.list_doc_month);
            int rc = dataGridView1.RowCount, hc = dataGridView1.ColumnCount ;


            DataTable g = new DataTable();
            g.Columns.Add("cod123", typeof(int));
            g.Columns.Add("name", typeof(string ));
            g.Columns.Add("width", typeof(string ));
            g.Columns.Add("caption", typeof(string));
            g.Columns.Add("ITOGfunction", typeof(string));
            g.Columns.Add("prim", typeof(string));
            g.AcceptChanges();

            DataRow r;

            r = g.NewRow();
            r[0] = 0;
            r[1] = "pro_headerReport";
            r[2] = 0;
            r[3] = 0;
            r[4] = "-";
            r[5] = "Заголовок отчета";
            g.Rows.Add(r);
            


            for (int j = 0; j < hc; j++)
            {
                r = g.NewRow();
                r[0] = j+1;
                r[1] = dataGridView1.Columns[j].Name;
                r[2] = dataGridView1.Columns[j].Width;
                r[3] = dataGridView1.Columns[j].HeaderText;
                r[4] = "-" ;
                r[5] = "наименование столбца";
                g.Rows.Add(r);
            //    
            }
            g.AcceptChanges();
            FormReportFromGRID f = new FormReportFromGRID(g);

            f.ShowDialog();

            if (f.IsOk)
            {
                h.m("save");
                q.sh(g);
            }
            else
            {
                h.m("do not save");
            }

            /*
            r = g.NewRow();
            r[0] = hc + 1;
            r[1] = "pro_hc";
            r[2] = 0;
            r[3] = hc;
            r[4] = "-";
            r[5] = "количество столцов";
            g.Rows.Add(r);

            r = g.NewRow();
            r[0] = hc+2;
            r[1] = "pro_rc";
            r[2] = 0;
            r[3] = rc;
            r[4] = "-";
            r[5] = "количество записей";
            g.Rows.Add(r);
            */

            

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cellclick();
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripDropDownButton2_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.work);
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            cellclick();
        }

        private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            toolStripTextBox1KeyUp();
        }

        private void setItog_PRIHOD_filter(DataTable dto, string fielNameFilter,string sfilter)
        {
            string flt = string.Concat(fielNameFilter," like '%", sfilter, "%'");
            /*codd,  
      dd, 
      codo,
      name_cod(z.name, ct) as ct, 
      kol, 
      kol2, 
      k, 
      round(pr,5) as pr, 
      round(sd,2) as sd*/
            delRows(dto, "ИТОГИ:", 2);
            int ic = dto.Rows.Count;
            DataRow r = dto.NewRow();
            // String.Format("{0:0.00}", pd);
            //dts.Rows.Remove(
           // h.m(22);
            r[(int)enumR.table_r4_rdtd_prihod._02_codo] = "ИТОГИ:";
            //try
            //{
           // h.m(23);
            r[(int)enumR.table_r4_rdtd_prihod._00_codd ] = 88888; //string.Format("{0:0.0}", dto.Compute("sum(sd)", flt));
            //    h.m(24);
            r[(int)enumR.table_r4_rdtd_prihod._08_sd] = string.Format("{0:0.0}", dto.Compute("sum(sd)", flt)); ;
            //    h.m(25);
                dto.Rows.Add(r);
            //    h.m(26);
                dto.AcceptChanges();
            //}
            //catch { }

        }
        private void delRows(DataTable dto, object sWhatDEL, int indexColumn)
        {
            ArrayList rowsToRemove = new ArrayList();
            for (int i = 0; i < dto.Rows.Count; i++)
            {
                if (dto.Rows[i][indexColumn].ToString() == sWhatDEL)
                    rowsToRemove.Add(dto.Rows[i]);
            }

            for (int j = 0; j < rowsToRemove.Count; j++)
            {
                dto.Rows.Remove((DataRow)rowsToRemove[j]);
            }
        }
        private void toolStripTextBox1KeyUp()
        {
            try
            {
                if (currentOpro == opro.lookDoc_RASHOD || currentOpro == opro.lookDoc_RASHODnr)
                {
                    DataView dv = new DataView(dts);
                    object os = dts.Compute("sum(k)", string.Concat("gn like '%", toolStripTextBox1.Text, "%'"));
                    lable_ss.Text = os.ToString();
                    dv.RowFilter = string.Concat("gn like '%", toolStripTextBox1.Text, "%'");
                    dataGridView1.DataSource = dv;
                }
                else if (currentOpro == opro.lookOstN900)
                {
                    DataView dv = new DataView(dts);
                    object os = dts.Compute("sum(sd)", string.Concat("name like '%", toolStripTextBox1.Text, "%'"));
                    lable_ss.Text = os.ToString();
                    dv.RowFilter = string.Concat("name like '%", toolStripTextBox1.Text, "%'");
                    dataGridView1.DataSource = dv;
                }
                else if (currentOpro == opro.lookDoc_PRIHOD)
                {
                    DataView dv = new DataView(dts); string sf = toolStripTextBox1.Text;
                    object os = dts.Compute("sum(k)", string.Concat("ct like '%", sf, "%'"));
                    lable_ss.Text = os.ToString();
                    setItog_PRIHOD_filter(dts, "codo" ,sf);
                    dv.RowFilter = string.Concat("codo like '%", sf, "%' or codo = 'ИТОГИ:'");
                    dataGridView1.DataSource = dv;
                }
                else if (currentOpro == opro.look_SirPer || currentOpro == opro.look_SirPer_Before)
                {
                    DataView dv = new DataView(dts);
                    object os = dts.Compute("sum(rob)", string.Concat("n0 like '%", toolStripTextBox1.Text, "%'"));
                    lable_ss.Text = os.ToString();
                    dv.RowFilter = string.Concat("n0 like '%", toolStripTextBox1.Text, "%'");
                    dataGridView1.DataSource = dv;
                }
                else if (currentOpro == opro.lookDoc_RASHODdays)
                {
                    DataView dv = new DataView(dts);
                    object os = dts.Compute("sum(kol)", string.Concat("gn like '%", toolStripTextBox1.Text, "%'"));
                    lable_ss.Text = os.ToString();
                    dv.RowFilter = string.Concat("gn like '%", toolStripTextBox1.Text, "%'");

                    dataGridView1.DataSource = dv;
                }
                else if (currentOpro == opro.lookAvgPr)
                {      DataView dv = new DataView(dts); //H.m(30);
                    object os = dts.Compute("sum(sm)", string.Concat("namet like '%", toolStripTextBox1.Text, "%'"));
                    object osk = dts.Compute("sum(k)", string.Concat("namet like '%", toolStripTextBox1.Text, "%'"));
                    object oskn = dts.Compute("sum(kn)", string.Concat("namet like '%", toolStripTextBox1.Text, "%'"));
                    double dap = Convert.ToDouble(os) / Convert.ToDouble(osk);
                    double dapn = Convert.ToDouble(os) / Convert.ToDouble(oskn);
                    lable_ss.Text = string.Concat(dap, " / ", dapn);
                    dv.RowFilter = string.Concat("namet like '%", toolStripTextBox1.Text, "%'");
                    dataGridView1.DataSource = dv;
                }
                else if (currentOpro == opro.look_r4_rdtd_prod)
                {
                    DataView dv = new DataView(dts);
                    object os = dts.Compute("sum(itog)", string.Concat("ct0 like '%", toolStripTextBox1.Text, "%'"));
                    object os2 = dts.Compute("sum(sht)", string.Concat("ct0 like '%", toolStripTextBox1.Text, "%'"));
                    try
                    {
                        lable_ss.Text = string.Concat("Итого шт=", Convert.ToInt32(os2), "  вес=", Convert.ToInt32(os));
                    }
                    catch { }
                    dv.RowFilter = string.Concat("ct0 like '%", toolStripTextBox1.Text, "%'");
                    dataGridView1.DataSource = dv;
                }
            }
            catch (Exception e)
            {
                h.m(e.Message);
            }
        }
      
        private void toolStripTextBox2_KeyUp(object sender, KeyEventArgs e)
        {
            toolStripTextBox2KeyUp();
        }


        private void toolStripTextBox2KeyUp()
        {
            if (currentOpro == opro.look_SirPer || currentOpro == opro.look_SirPer_Before)
            {
                DataView dv = new DataView(dts); //H.m(30);
                object os = dts.Compute("sum(rob)", string.Concat("n44 like '%", toolStripTextBox2.Text, "%'"));
                lable_ss.Text = os.ToString();
                dv.RowFilter = string.Concat("n44 like '%", toolStripTextBox2.Text, "%'");
                dataGridView1.DataSource = dv;
            }
            else if (currentOpro == opro.lookDoc_RASHODdays)
            {
                DataView dv = new DataView(dts); //H.m(30);
                object os = dts.Compute("sum(kol)", string.Concat("ct like '%", toolStripTextBox2.Text, "%'"));
                lable_ss.Text = os.ToString();
                dv.RowFilter = string.Concat("ct like '%", toolStripTextBox2.Text, "%'");
                dataGridView1.DataSource = dv;
            }
            else if (currentOpro == opro.lookDoc_PRIHOD )
            {
                DataView dv = new DataView(dts); string sf = toolStripTextBox2.Text;//H.m(30);
                object os = dts.Compute("sum(kol)", string.Concat("ct like '%", toolStripTextBox2.Text, "%'"));
                lable_ss.Text = os.ToString();
                setItog_PRIHOD_filter(dts, "ct", sf);
                dv.RowFilter = string.Concat("ct like '%", sf, "%' or codo = 'ИТОГИ:'");
                dataGridView1.DataSource = dv;
            }
            else if (currentOpro == opro.lookAvgPr )
            {
                /*   object os = dts.Compute("sum(pr*k)/sum(k)", string.Concat("namet like '%", toolStripTextBox1.Text, "%'"));
                    object ss = dts.Compute("sum(prpr*k/ep)/sum(k/ep)", string.Concat("namet like '%", toolStripTextBox1.Text, "%'"));
                */

                DataView dv = new DataView(dts); //H.m(30);
                object os = 0;
                object osk = 0;
                object oskn = 0;
                double dap = 0;
                double dapn = 0;
                try
                {
                    os = dts.Compute("sum(sm)", string.Concat("name like '%", toolStripTextBox2.Text, "%'"));
                    osk = dts.Compute("sum(k)", string.Concat("name like '%", toolStripTextBox2.Text, "%'"));
                    oskn = dts.Compute("sum(kn)", string.Concat("name like '%", toolStripTextBox2.Text, "%'"));
                    dap = Convert.ToDouble(os) / Convert.ToDouble(osk);
                    dapn = Convert.ToDouble(os) / Convert.ToDouble(oskn);
                }
                catch { }
                lable_ss.Text = string.Concat(  dap ," / ", dapn);
                dv.RowFilter = string.Concat("name like '%", toolStripTextBox2.Text, "%'");
                dataGridView1.DataSource = dv;
            }
            else if ( currentOpro == opro.lookDoc_RASHOD)
            {
                DataView dv = new DataView(dts); //H.m(30);
                object os = dts.Compute("sum(kol)", string.Concat("gn like '%", toolStripTextBox2.Text, "%'"));
                lable_ss.Text = os.ToString();
                dv.RowFilter = string.Concat("gn like '%", toolStripTextBox2.Text, "%'");
                dataGridView1.DataSource = dv;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            toolStripMenuItem2_Click_fun();
        }
        public void toolStripMenuItem2_Click_fun()
        {
          //  string[] aa = 
            //string[] aa = new string[]
            //    {
            //    "Наименование0", "Наименование1", "КолР1",
            //    "Наименование2", "Кол2", "КолВых2",
            //    "Наименование2с", "КолР2", "Наименование3", "Кол3", "КолВ3", "Наименование3с", "КолР3",
            //    "Наименование4", "Кол4", "КолВ4", "КолР4", "Наименование", "КолРасх", "КодТ"
            //    };
            


            SaveFileDialog SV = new SaveFileDialog();
            SV.Filter = "MSExcel files (*.xls)|*.xls|All files (*.*)|*.*";
            SV.FilterIndex = 1;
            SV.RestoreDirectory = true;
            if (SV.ShowDialog() == DialogResult.OK)
            {
                saveDataXLS(SV.FileName.ToString());
                MessageBox.Show(string.Concat("Экспорт в файл ", SV.FileName.ToString(), " успешно завершен."));
            }
        } 
        public void saveDataXLS(string filePath)
        {


            string file_path = filePath;
            int clmns = dataGridView1.Columns.Count;
            Int32 rows = dataGridView1.Rows.Count;
            Int32 i = 0;
            Int32 i1 = 0;
            string clType = "String";//"String";

            string[] aa = new string[clmns];
            for (int j = 0; j < clmns; j++) { aa[j] = dataGridView1.Columns[j].HeaderText; }
            
            
            System.IO.StreamWriter file = new System.IO.StreamWriter(file_path);
            file.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            file.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
            file.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
            file.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
            file.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
            file.WriteLine(" <Styles>");
            file.WriteLine("  <Style ss:ID=\"s21\">");
            file.WriteLine("   <NumberFormat ss:Format=\"Fixed\"/>");
            file.WriteLine("  </Style>");
            file.WriteLine(" </Styles>");
            file.WriteLine(" <Worksheet ss:Name=\"Table1\">");
            file.WriteLine("  <Table>");
            file.WriteLine("   <Row>");
            for (i = 0; i < clmns; i++)
            {
                file.WriteLine(string.Concat("    <Cell><Data ss:Type=\"String\">", aa[i], "</Data></Cell>"));
            }
            file.WriteLine("   </Row>");
            for (i1 = 0; i1 < rows; i1++)
            {
                file.WriteLine("   <Row>");
                for (i = 0; i < clmns; i++)
                {
                    file.WriteLine(string.Concat("    <Cell><Data ss:Type=\"", clType, "\">", dataGridView1.Rows[i1].Cells[i].Value, "</Data></Cell>"));
                }
                file.WriteLine("   </Row>");
            }
            file.WriteLine("  </Table>");
            file.WriteLine(" </Worksheet>");
            file.WriteLine("</Workbook>");
            file.Close();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            prepReport();
            printPreviewDialog1.ShowDialog();
            //grid_print();
        }

        private void prepReport()
        {
            if (currentOpro == opro.lookDoc_RASHOD)
            {
                 prepReport_if(opro.lookDoc_RASHOD);
            }
            else if (currentOpro == opro.lookDoc_PRIHOD)
            {

            }
            else if (currentOpro == opro.lookDoc_RASHODdays)
            {
                // vv.RPT.currentReport 
                currentReport = reports.allDocTD_group;
                vv.RPT.showCod_1yes_0no = "1";
                vv.RPT.stringControlGroup = ""; vv.RPT.lp = 0; vv.RPT.yb = 0; vv.RPT.col = 0; currentReport = reports.allDocTD_group;
                void_lookDoc_RASHODdays();
            }
        }

        private void void_lookDoc_RASHODdays()
        {
            currentPage = 0; 
            printPreviewDialog1.ShowDialog();
        }

        void load_if_thereIs(opro op)
        {
 
        }
       
        void prepReport_if(opro op)
        {
            
            vv.PRINT_GRID.boolLoadFromXML = lookDir(op.ToString());
          
            
            if (vv.PRINT_GRID.boolLoadFromXML)
            {
              //  h.m(op.ToString(), "  yes");
                vv.PRINT_GRID.tableParamReport = readXMLtoDataTable(op.ToString());
                vv.PRINT_GRID.loadParamFromXMLTable_and_loadDataViewToTable(dataGridView1);
                currentReport = reports.fromGrid;
            }
            else
            {
                currentReport = reports.fromGrid;
               // h.m(op.ToString(), "  no");
                saveReportParamToXML_first();          
            }
        }
        private void saveReportParamToXML_first()
        {
            vv.PRINT_GRID.aCaptions = new string[dataGridView1.ColumnCount];
            vv.PRINT_GRID.aWidth = new int[dataGridView1.ColumnCount];
            vv.PRINT_GRID.fontSizeRows = 8;
            vv.PRINT_GRID.fontSizeHeader = 20;
            vv.PRINT_GRID.heightRows = 18;

            for (int j = 0; j < dataGridView1.ColumnCount; j++)
            {
                vv.PRINT_GRID.aCaptions[j] = dataGridView1.Columns[j].HeaderText;
                vv.PRINT_GRID.aWidth[j] = dataGridView1.Columns[j].Width;
            }
            createTableParamFirst(currentOpro.ToString());
        }


        private void createTableParamFirst(string fileNameWithoutXML)
        {
            DataTable t = new DataTable("tp");
            t.Columns.Add("cod", typeof(int));
            t.Columns.Add("name", typeof(string));
            t.Columns.Add("prim", typeof(string));
            t.AcceptChanges();
           

            DataRow r;
            r = t.NewRow();
            r[0] = 1; r[1] = vv.PRINT_GRID.tagCAPTIONS; r[2] = "aCaptions";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 2; r[1] = vv.PRINT_GRID.tagWIDTH; r[2] = "aWidth";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 3; r[1] = vv.PRINT_GRID.heightRows; r[2] = "heightRows";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 4; r[1] = vv.PRINT_GRID.fontSizeRows; r[2] = "fontSizeRows";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 5; r[1] = vv.PRINT_GRID.fontSizeHeader; r[2] = "fontSizeHeader";
            t.Rows.Add(r);
            t.Columns[0].AutoIncrement = true;
            t.AcceptChanges();

            t.WriteXml( vv.PRINT_GRID.filePLP_REPORTS(fileNameWithoutXML),
                        XmlWriteMode.WriteSchema);
        }

        private void saveReportParamToXML(string fileName)
        {
            string s = string.Concat("PLP\\REPORTS\\", fileName, ".xml");
            vv.PRINT_GRID.tableParamReport.WriteXml(s, XmlWriteMode.WriteSchema);
        }
       
        private DataTable lookDir_And_ReadFile_To_DataTable(string fileName)
        {
            DataTable t = new DataTable();
            string s = "";
            if (lookDir(fileName))
            {
                s = string.Concat("PLP\\REPORTS\\", fileName, ".xml");
                t.ReadXml(s);
                return t;
            }
            else return null;
        }
        private DataTable readXMLtoDataTable(string fileName)
        {
            DataTable t = new DataTable();
                t.ReadXml(string.Concat("PLP\\REPORTS\\", fileName, ".xml"));
                return t;
        }
        private bool lookDir(string fileName)
        {
            bool IsHere = false;
            DirectoryInfo di = new DirectoryInfo("PLP\\REPORTS");
            FileInfo[] fai = di.GetFiles();
            foreach (FileInfo fi in fai)
            {
                if (fileName == fi.Name.Replace(".xml", "")) { 
                    IsHere = true; break; }
            }
            return IsHere;
        }
        private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            currentPage = 0;
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (currentReport == reports.fromGrid)
            {
                if (vv.PRINT_GRID.boolLoadFromXML)
                { grid2_print_prp(e); }
                else { grid2_print_prp(e, dataGridView1); }
            }
            else if (currentReport == reports.allDocTD_group )
            {
                allDocTD_group_print(e);
            }
        }
        private string nn(object o)
        {
            string s = "0";
            if (Convert.ToInt32(o) < 10) { s = string.Concat("0", o); } else { s = o.ToString(); }
            return s;
        }
        private string n(object o)
        {
            string s = "";
            if (o != null)
            {
                if (o.ToString() != "0" && o.ToString() != "-") { s = o.ToString(); }
            }
                return s;
        }
        private void grid2_print_prp(System.Drawing.Printing.PrintPageEventArgs e)
        {
            //DataTable t = new DataTable();
            int RowCount = vv.PRINT_GRID.tableSourseReport.Rows.Count ,
                ColumnCount = vv.PRINT_GRID.tableSourseReport.Columns.Count;
            //string[] ColumnCaption = new string[ColumnCount];
            //int[] ColumnWidth = new int[ColumnCount];
            //t = (DataTable)dgv.DataSource;

            //for (int j = 0; j < ColumnCount; j++) 
            //{ ColumnCaption[j] = dgv.Columns[j].HeaderText; ColumnWidth[j] = dgv.Columns[j].Width; }

            //grid2_print(e, "Таблица", t, ColumnCaption, ColumnWidth, 20, 8);

            grid2_print(e, "Таблица", 
              vv.PRINT_GRID.tableSourseReport,
              vv.PRINT_GRID.aCaptions,
              vv.PRINT_GRID.aWidth , vv.PRINT_GRID.heightRows, vv.PRINT_GRID.fontSizeRows);

            // q.sh(t);
        }

        private void grid2_print_prp(System.Drawing.Printing.PrintPageEventArgs e,DataGridView dgv)
        {
            DataTable t  = new DataTable();
            int RowCount = dgv.RowCount, ColumnCount = dgv.ColumnCount;
            string[] ColumnCaption = new string[ColumnCount];
            int[] ColumnWidth = new int[ColumnCount];
            t = (DataTable)dgv.DataSource;

            for (int j = 0; j < ColumnCount; j++) { ColumnCaption[j] = dgv.Columns[j].HeaderText; ColumnWidth[j] = dgv.Columns[j].Width; }

            grid2_print(e,  "Таблица", t, ColumnCaption, ColumnWidth, 20, 8);
           // q.sh(t);
        }
        private void grid2_print(System.Drawing.Printing.PrintPageEventArgs e, 
            string ReportHeader, 
            DataTable dto, string[] ColumnCaption,
           int[] ColumnWidth, int RowsHeight, int FontSizeInRows)
        {
            Graphics g = e.Graphics; string oo = ""; string om = "";

            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

            int xr = 40, yr = 15, x = 20, y = 25 + yr;

            #region  Rectangle and Width and Heght and fornt

            int icc = dto.Columns.Count;
            rra = new Rectangle[icc];

            Rectangle rg, rs;
            Rectangle rHeadline, rPageNumber;
            //wwa = new int[dataGridView1.ColumnCount]; for (int j = 0; j < icc; j++) { wwa[j] = dataGridView1.Columns[j].Width; }


            int wg = 220, ww = 30, wk = 55;
            int w0 = 30;
            int w1 = 280, w2 = wk, w3 = wk, w4 = wk, w5 = wk,
                w6 = wk, w7 = wk, w8 = wk + 10,
                w9 = wk, w10 = 15;
            int h = RowsHeight, hh = 5, f = FontSizeInRows, ff = 10;
            int hg = 20;
            #endregion

            #region other declaration
            int ic = dto.Rows.Count;// dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat(ReportHeader);
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okosh = 0, okol = 0;

            //---------------------- Итоги
            #region r1---r21


            #endregion

            #region drawStr
            #endregion
            //----------------------

            y = y + h;
            //---------------------- Caption
            #region ---------------------- Caption
            int wwaa = 0;
            for (int k = 0; k < icc; k++)
            {
                rra[k] = new Rectangle(x + wwaa, y, ColumnWidth[k], h);
                if (k == 0) { wwaa = ColumnWidth[k]; } else { wwaa = wwaa + ColumnWidth[k]; }
                g.DrawString(ColumnCaption[k], new Font("Arial", f), Brushes.Black, rra[k], sfc);
            }
            #endregion
            //----------------------

            y = y + h; vv.RPT.yb = y;
            while (linesPrint < ic)
            {

                #region r1---r21

                wwaa = 0;
                for (int k = 0; k < icc; k++)
                {
                    rra[k] = new Rectangle(x + wwaa, y, ColumnWidth[k], h);
                    if (k == 0) { wwaa = ColumnWidth[k]; } else { wwaa = wwaa + ColumnWidth[k]; }
                    g.DrawRectangle(Pens.LightSteelBlue, rra[k]);
                    g.DrawString(n(dto.Rows[linesPrint][k]), new Font("Arial", f), Brushes.Black, rra[k]);
                }


                #endregion


                y += h; linesPrint++; vv.RPT.lp++;
                if (y >= e.PageBounds.Height - 50)
                { e.HasMorePages = true; return; }
            }
            linesPrint = 0; vv.RPT.col = 0; e.HasMorePages = false;
        }
        private void grid2_print(System.Drawing.Printing.PrintPageEventArgs e, DataTable dto, string[] ColumnCaption,
            int[] ColumnWidth)
        {
            Graphics g = e.Graphics; string oo = ""; string om = "";

            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

            int xr = 40, yr = 15, x = 20, y = 25 + yr;

            #region  Rectangle and Width and Heght and fornt

            int icc = dto.Columns.Count ;
            rra = new Rectangle[icc];

            Rectangle rg, rs;
            Rectangle rHeadline, rPageNumber;
            //wwa = new int[dataGridView1.ColumnCount]; for (int j = 0; j < icc; j++) { wwa[j] = dataGridView1.Columns[j].Width; }


            int wg = 220, ww = 30, wk = 55;
            int w0 = 30;
            int w1 = 280, w2 = wk, w3 = wk, w4 = wk, w5 = wk,
                w6 = wk, w7 = wk, w8 = wk + 10,
                w9 = wk, w10 = 15;
            int h = 15, hh = 5, f = 8, ff = 10;
            int hg = 20;
            #endregion

            #region other declaration
            int ic = dto.Rows.Count;// dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat("--- ");
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okosh = 0, okol = 0;

            //---------------------- Итоги
            #region r1---r21


            #endregion

            #region drawStr
            #endregion
            //----------------------

            y = y + h;
            //---------------------- Caption
            #region ---------------------- Caption
            int wwaa = 0;
            for (int k = 0; k < icc; k++)
            {
                rra[k] = new Rectangle(x + wwaa, y, ColumnWidth[k], h);
                if (k == 0) { wwaa = ColumnWidth[k]; } else { wwaa = wwaa + ColumnWidth[k]; }
                g.DrawString(ColumnCaption[k], new Font("Arial", f), Brushes.Black, rra[k], sfc);
            }
            #endregion
            //----------------------

            y = y + h; vv.RPT.yb = y;
            while (linesPrint < ic)
            {

                #region r1---r21

                wwaa = 0;
                for (int k = 0; k < icc; k++)
                {
                    rra[k] = new Rectangle(x + wwaa, y, ColumnWidth[k], h);
                    if (k == 0) { wwaa = ColumnWidth[k]; } else { wwaa = wwaa + ColumnWidth[k]; }
                    g.DrawRectangle(Pens.LightSteelBlue, rra[k]);
                    g.DrawString(n(dto.Rows[linesPrint][k]), new Font("Arial", f), Brushes.Black, rra[k]);
                }


                #endregion

               
                y += h; linesPrint++; vv.RPT.lp++;
                if (y >= e.PageBounds.Height - 50)
                { e.HasMorePages = true; return; }
            }
            linesPrint = 0; vv.RPT.col = 0; e.HasMorePages = false;
        }


        private void grid_print(System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics; string oo = ""; string om = "";

            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

            int xr = 40, yr = 15, x = 20, y = 25 + yr;

            #region  Rectangle and Width and Heght and fornt
      
            int icc = dataGridView1.ColumnCount;
            rra = new Rectangle[icc];
                
            Rectangle rg, rs;
            Rectangle rHeadline, rPageNumber;
            wwa  = new int[dataGridView1.ColumnCount];  for (int j = 0; j < icc; j++)  { wwa[j] = dataGridView1.Columns[j].Width;   }


            int wg = 220, ww = 30, wk = 55;
            int w0 = 30;
            int w1 = 280, w2 = wk, w3 = wk, w4 = wk, w5 = wk,
                w6 = wk, w7 = wk, w8 = wk + 10,
                w9 = wk, w10 = 15;
            int h = 15, hh = 5, f = 8, ff = 10;
            int hg = 20;
            #endregion

            #region other declaration
            int ic = dataGridView1.RowCount;// dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat("--- ");
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okosh = 0, okol = 0;

            //---------------------- Итоги
            #region r1---r21


            #endregion

            #region drawStr
            #endregion
            //----------------------

            y = y + h;
            //---------------------- Caption
            #region ---------------------- Caption
            int wwaa = 0;
            for (int k = 0; k < icc; k++)
            {

                rra[k] = new Rectangle(x + wwaa, y, wwa[k], h);
                if (k == 0) { wwaa = wwa[k]; } else { wwaa = wwaa + wwa[k]; }
                     g.DrawString(dataGridView1.Columns[k].HeaderText,
                       new Font("Arial", f), Brushes.Black, rra[k], sfc);
            }

           
            #endregion
            //----------------------

            y = y + h; vv.RPT.yb = y;
            while (linesPrint < ic)
            {

                #region r1---r21

                wwaa = 0;
                for (int k = 0; k < icc; k++)
                {
                    rra[k] = new Rectangle(x + wwaa, y, wwa[k], h);
                    if (k == 0) { wwaa = wwa[k]; } else { wwaa = wwaa + wwa[k]; }
                    g.DrawRectangle(Pens.LightSteelBlue, rra[k]);
                    g.DrawString(n(dataGridView1.Rows[linesPrint].Cells[k].Value),
                        new Font("Arial", f), Brushes.Black, rra[k]);
                }


                #endregion

                #region drawStr
                //      String.Format("{0:0.00}", 123.4567);
                #endregion

                y += h; linesPrint++; vv.RPT.lp++;
                if (y >= e.PageBounds.Height - 50)
                { e.HasMorePages = true; return; }
            }
            linesPrint = 0; vv.RPT.col = 0; e.HasMorePages = false;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
           // grid_to_table(dataGridView1);
            
        }


        private void allDocTD_print(System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics; string oo = ""; string om = "";

            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

            int xr = 40, yr = 15, x = 20, y = 25 + yr;
            #region  Rectangle and Width and Heght and fornt


            //int w1 = 29, w2 = 250, w3 = 42, w4 = 42;
            //int wg1 = w3 + 7, wg2 = w4;
            //int wg = w1 + w2 + w3 + w4 - (wg1 + wg2), ww = 9, wgw = wg + wg1 + wg2;
            //int h = 25, hh = 5, f = 12, ff = 10, fg = f - 2;
            //int hg = 38;
            #endregion
            #region  Rectangle and Width and Heght and fornt
            Rectangle r1, r2, r3, r4, r5;
            Rectangle rg, rg1, rg2;
            Rectangle rs;
            Rectangle rHeadline, rPageNumber;

            int w1 = 25, w2 = 80, w3 = 200, w4 = 60;
            int wg1 = w3 + 7, wg2 = w4;
            int wg = w1 + w2 + w3 + w4 - (wg1 + wg2), ww = 10, wgw = wg + wg1 + wg2; // wgg = wg - wg1 - wg2, 
            //int wg = w1 + w2 + w3 + w4 - (wg1 + wg2), ww = 9, wgw = wg + wg1 + wg2;
            int h = 20, hh = 5, f = 12, ff = 10, fn = 8, fg = f - 2;
            // int h = 20, hh = 5, f = 10, ff = 10;
            int hg = 22;
            #endregion

            #region other declaration
            int ic = dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat("Усть-Орда. Заявка на ", 0
                , " ");
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okol = 0, gkol = 0;
            // okosh = dts.Compute("SUM(kosh)", "kosh>=0");
            okol = dts.Compute("SUM(kol)", "kol>=0");
            //---------------------- Итоги
            #region r1---r21

            rg = new Rectangle(x + vv.RPT.col * wgw, y, wg, h);
            rg1 = new Rectangle(x + vv.RPT.col * (wgw + ww) + wg, y, wg1, h);
            rg2 = new Rectangle(x + vv.RPT.col * (wgw + ww) + wg + wg1, y, wg2, h);
            //  r4 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1 + w2 + w3, y, w4, h);
            // r5 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1 + w2 + w3 + w4, y, w5, h);

            #endregion

            #region drawStr
            g.DrawString("Итого : ", new Font("Arial", f), Brushes.Black, rg);
            g.DrawString("", new Font("Arial", f), Brushes.Black, rg1, sfc);
            g.DrawString(okol.ToString(), new Font("Arial", f), Brushes.Black, rg2, sfc);
            #endregion
            //----------------------

            y = y + h;
            //---------------------- Caption
            #region r1---r21

            r1 = new Rectangle(x + vv.RPT.col * (wgw + ww), y, w1, h);
            r2 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1, y, w2, h);
            r3 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1 + w2, y, w3, h);
            r4 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1 + w2 + w3, y, w4, h);
            //r5 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1 + w2 + w3 + w4, y, w5, h);

            g.DrawRectangle(Pens.LightSteelBlue, r1);
            g.DrawRectangle(Pens.LightSteelBlue, r2);
            g.DrawRectangle(Pens.LightSteelBlue, r3);
            g.DrawRectangle(Pens.LightSteelBlue, r4);
            //g.DrawRectangle(Pens.LightSteelBlue, r5);


            #endregion

            #region drawStr
            g.DrawString("№пп", new Font("Arial", f), Brushes.Black, r1, sfc);
            g.DrawString("Дата ", new Font("Arial", f), Brushes.Black, r2, sfc);
            g.DrawString("Наименование", new Font("Arial", f), Brushes.Black, r3, sfc);
            g.DrawString("кол", new Font("Arial", f), Brushes.Black, r4, sfc);
            #endregion
            //----------------------

            y = y + h; vv.RPT.yb = y;
            while (linesPrint < ic)
            {
               
                        #region r1---r21
                        r1 = new Rectangle(x + vv.RPT.col * (wgw + ww), y, w1, h);
                        r2 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1, y, w2, h);
                        r3 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1 + w2, y, w3, h);
                        r4 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1 + w2 + w3, y, w4, h);

                        g.DrawRectangle(Pens.LightSteelBlue, r1);
                        g.DrawRectangle(Pens.LightSteelBlue, r2);
                        g.DrawRectangle(Pens.LightSteelBlue, r3);
                        g.DrawRectangle(Pens.LightSteelBlue, r4);

                        #endregion

                        #region drawStr
                        g.DrawString(n(linesPrint + 1), new Font("Arial", fn), Brushes.Black, r1);

                       
                            vv.RPT.nameProd = n(dts.Rows[linesPrint][0]);
                       
                        g.DrawString(vv.RPT.nameProd, new Font("Arial", fg, FontStyle.Bold), Brushes.Black, r2);
                        g.DrawString(n(dts.Rows[linesPrint][1]), new Font("Arial", f), Brushes.Black, r3);
                        g.DrawString(n(dts.Rows[linesPrint][2]), new Font("Arial", f), Brushes.Black, r4);
                        #endregion

                        y += h; linesPrint++; vv.RPT.lp++;
                        if ((y >= e.PageBounds.Height - 50) && (vv.RPT.col == 1))
                        { e.HasMorePages = true; vv.RPT.col = 0; return; }
                        else if (y >= e.PageBounds.Height - 50)
                        { y = vv.RPT.yb; vv.RPT.col++; }
              
            }
            linesPrint = 0; vv.RPT.col = 0; e.HasMorePages = false;
        }


        private void allDocTD_group_print(System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics; string oo = ""; string om = "";

            currentPage++;
            //$$$ Настройка страницы            
            e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

            int xr = 40, yr = 15, x = 20, y = 25 + yr;
            #region  Rectangle and Width and Heght and fornt


            //int w1 = 29, w2 = 250, w3 = 42, w4 = 42;
            //int wg1 = w3 + 7, wg2 = w4;
            //int wg = w1 + w2 + w3 + w4 - (wg1 + wg2), ww = 9, wgw = wg + wg1 + wg2;
            //int h = 25, hh = 5, f = 12, ff = 10, fg = f - 2;
            //int hg = 38;
            #endregion
            #region  Rectangle and Width and Heght and fornt
            Rectangle r1, r2, r3, r4, r5;
            Rectangle rg, rg1, rg2;
            Rectangle rs;
            Rectangle rHeadline, rPageNumber;

            int w1 = 25, w2 = 80, w3 = 0, w4 = 45;
            int wg1 = w3 + 7, wg2 = w4;
            int wg = w1 + w2 + w3 + w4 - (wg1 + wg2), ww = 10, wgw = wg + wg1 + wg2; // wgg = wg - wg1 - wg2, 
            //int wg = w1 + w2 + w3 + w4 - (wg1 + wg2), ww = 9, wgw = wg + wg1 + wg2;
            int h = 20, hh = 5, f = 9, ff = 10, fn = 8, fg = f;// -2;
            // int h = 20, hh = 5, f = 10, ff = 10;
            int hg = 48;
            #endregion
            
            #region other declaration
            int ic = dts.Rows.Count;
            string n0 = "", n5 = "", n10 = "", n15 = "";
            colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;
            string sz = string.Concat("Сверка по дням");
            #endregion

            #region Aligment
            System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
            System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
            System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
            #endregion

            rHeadline = new Rectangle(xr, yr, 500, h + 5);
            rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

            if (currentPage == 1) g.DrawString(sz, new Font("Arial", 12), Brushes.Black, rHeadline);

            g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);
            object okol = 0, gkol = 0;
           // okosh = dts.Compute("SUM(kosh)", "kosh>=0");
            okol = dts.Compute("SUM(kol)", "kol>=0");
            //---------------------- Итоги
            #region r1---r21

            rg = new Rectangle(x + vv.RPT.col * wgw, y, 80, h+7);
            rg1 = new Rectangle(x + vv.RPT.col * (wgw + ww) + wg, y, 0, h+7);
            rg2 = new Rectangle(x + vv.RPT.col * (wgw + ww) + wg + 0, y, w1 + w2 + w3 + w4-80, h + 7);
            //  r4 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1 + w2 + w3, y, w4, h);
            // r5 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1 + w2 + w3 + w4, y, w5, h);

            #endregion

            #region drawStr
            g.DrawString("Итого : ", new Font("Arial", f), Brushes.Black, rg);
            g.DrawString("", new Font("Arial", f), Brushes.Black, rg1, sfc);
            g.DrawString(okol.ToString(), new Font("Arial", f), Brushes.Black, rg2, sfc);
            #endregion
            //----------------------

            y = y + h+7;
            //---------------------- Caption
            #region r1---r21

            r1 = new Rectangle(x + vv.RPT.col * (wgw + ww), y, w1, h);
            r2 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1, y, w2, h);
            r3 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1 + w2, y, 0, h);
            r4 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1 + w2 + 0, y, w4, h);
            //r5 = new Rectangle(x + vv.RPT.col * (wg + ww) + w1 + w2 + w3 + w4, y, w5, h);

            g.DrawRectangle(Pens.LightSteelBlue, r1);
            g.DrawRectangle(Pens.LightSteelBlue, r2);
           // g.DrawRectangle(Pens.LightSteelBlue, r3);
            g.DrawRectangle(Pens.LightSteelBlue, r4);
            //g.DrawRectangle(Pens.LightSteelBlue, r5);


            #endregion

            #region drawStr
            g.DrawString("№пп", new Font("Arial", f), Brushes.Black, r1, sfc);
            g.DrawString("Дата", new Font("Arial", f), Brushes.Black, r2, sfc);
            g.DrawString("", new Font("Arial", f), Brushes.Black, r3, sfc);
            g.DrawString("кол", new Font("Arial", f), Brushes.Black, r4, sfc);
            #endregion
            //----------------------

            y = y + h; vv.RPT.yb = y;
            while (linesPrint < ic)
            {

                if (vv.RPT.lp == 0)
                {
                    rg = new Rectangle(x + vv.RPT.col * (wgw + ww), y, wg, hg);
                    rg1 = new Rectangle(x + vv.RPT.col * (wgw + ww) + wg, y, 0, hg);
                    rg2 = new Rectangle(x + vv.RPT.col * (wgw + ww) + wg + 0, y, wg2, hg);

                    vv.RPT.stringControlGroup = n(dts.Rows[linesPrint][0]).ToString();
                    vv.RPT.nameProd = vv.RPT.stringControlGroup;
                  
                    g.DrawString(vv.RPT.nameProd, new Font("Arial", fg), Brushes.Black, rg);

                    vv.RPT.okol = dts.Compute("SUM(kol)", string.Concat("gn='", vv.RPT.stringControlGroup, "'"));

                    g.DrawString(vv.RPT.okol.ToString(), new Font("Arial", fg, FontStyle.Bold), Brushes.Black, rg2);
                    y = y + hg ; vv.RPT.lp++;
                }
                else
                {
                    if (vv.RPT.stringControlGroup == n(dts.Rows[linesPrint][0]).ToString())
                    {
                        #region r1---r21
                        r1 = new Rectangle(x + vv.RPT.col * (wgw + ww), y, w1, h);
                        r2 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1, y, w2, h);
                        r3 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1 + w2, y, 0, h);
                        r4 = new Rectangle(x + vv.RPT.col * (wgw + ww) + w1 + w2 + 0, y, w4, h);

                        g.DrawRectangle(Pens.LightSteelBlue, r1);
                        g.DrawRectangle(Pens.LightSteelBlue, r2);
                        g.DrawRectangle(Pens.LightSteelBlue, r3);
                        g.DrawRectangle(Pens.LightSteelBlue, r4);

                        #endregion

                        #region drawStr
                        g.DrawString(n(linesPrint + 1), new Font("Arial", fn), Brushes.Black, r1);

                        
                        vv.RPT.nameProd = n(Convert.ToDateTime(  dts.Rows[linesPrint][1]).ToShortDateString());
                        
                        g.DrawString(vv.RPT.nameProd, new Font("Arial", fg), Brushes.Black, r2);
                        g.DrawString("", new Font("Arial", f), Brushes.Black, r3);
                        g.DrawString(n(dts.Rows[linesPrint][2]), new Font("Arial", f), Brushes.Black, r4, sfo);
                        #endregion

                        y += h; linesPrint++; vv.RPT.lp++;
                        if ((y >= e.PageBounds.Height - 50) && (vv.RPT.col == 4))
                        { e.HasMorePages = true; vv.RPT.col = 0; return; }
                        else if (y >= e.PageBounds.Height - 50)
                        { y = vv.RPT.yb; vv.RPT.col++; }
                    }
                    else
                    {
                        /*
                        rg = new Rectangle(x + vv.RPT.col * (wg + ww), y, wg, hg);
                        vv.RPT.stringControlGroup = 
                            n(tz.Rows[linesPrint][0]).ToString();
                        g.DrawString(vv.RPT.stringControlGroup, new Font("Arial", f, FontStyle.Bold), Brushes.Black, rg);
                        */
                        rg = new Rectangle(x + vv.RPT.col * (wgw + ww), y, wg, hg);
                        rg1 = new Rectangle(x + vv.RPT.col * (wgw + ww) + wg, y, 0, hg);
                        rg2 = new Rectangle(x + vv.RPT.col * (wgw + ww) + wg + 0, y, wg2, hg);

                        vv.RPT.stringControlGroup = n(dts.Rows[linesPrint][0]).ToString();

                        
                            vv.RPT.nameProd = vv.RPT.stringControlGroup;
                       
                        g.DrawString(vv.RPT.nameProd, new Font("Arial", fg), Brushes.Black, rg);

                        vv.RPT.okol = dts.Compute("SUM(kol)", string.Concat("gn='", vv.RPT.stringControlGroup, "'"));

                        g.DrawString(vv.RPT.okol.ToString(), new Font("Arial", fg, FontStyle.Bold), Brushes.Black, rg2, sfo );


                        y = y + hg; vv.RPT.lp++;
                        if (y >= e.PageBounds.Height - 50 && vv.RPT.col > 0)
                        { e.HasMorePages = true; vv.RPT.col = 0; return; }
                        else if (y >= e.PageBounds.Height - 50)
                        { y = vv.RPT.yb; vv.RPT.col++; }
                    }
                }
            }
            linesPrint = 0; vv.RPT.col = 0; e.HasMorePages = false;
        }

        private void toolStripMenuItem2_Click_1(object sender, EventArgs e)
        {
            vv.PRINT_GRID.tableParamReport = lookDir_And_ReadFile_To_DataTable(currentOpro.ToString());
            q.sh(vv.PRINT_GRID.tableParamReport);
            saveReportParamToXML(currentOpro.ToString());
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            setMode(rzpt.modeWAED.edit);
        }
    }
}
